package com.haoshuang.zhouzhoubang.ui.page.order.appeal.detail

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityAppealDetailBinding
import com.haoshuang.zhouzhoubang.ui.page.order.appeal.AppealActivity
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.ktx.toDateString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible

/**
 *  
 */
class AppealDetailActivity : BaseVMActivity<AppealDetailViewModel, ActivityAppealDetailBinding>() {

   companion object{
       fun launch(
           context: Context,
           order_id: Int?,
           detail: String?,
           images: String?,
       ) {
           context.startPage<AppealDetailActivity>(
               "order_id" to order_id,
               "detail" to detail,
               "images" to images
           )
       }
   }

    private val detail by lazy { intent.getString("detail") }
    private val images by lazy { intent.getString("images") }

    private val order_id: Int by lazy { intent.getIntExtra("order_id", 0) }

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply{
            titleBar.title = "差评申诉详情"
            titleBar.leftImageView().click { finish() }

            binding.tvDetail.text = detail
            binding.customUploadView.setMediaList(
                images.split(",").map { UploadFileBean(null, it, 0, false, 2) })
        }
    }

    override fun initData() {
        vm.userOrderAppealInfo(order_id)
    }

    override fun startObserve() {
        vm.userOrderAppealInfoData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                binding.apply {
                    tvAppealTime.text = ((it?.add_time?:0) * 1000).toDateString("yyyy-MM-dd HH:mm")
                    tvAppealReason.text = it?.content
                    cuvAppealEvidence.setMediaList(it?.images?.map { UploadFileBean(url = it) }?: mutableListOf())
                    when(it?.status){
                        0 -> {
                            // 待审核
                            tvStatus.text = "待审核"
                            llAuditTime.gone()
                            llRejectReason.gone()
                        }
                        1 -> {
                            tvStatus.text = "已通过"
                            llAuditTime.visible()
                            llRejectReason.gone()
                            tvAuditTime.text = ((it.check_time?:0) * 1000).toDateString("yyyy-MM-dd HH:mm")
                        }
                        2 -> {
                            tvStatus.text = "已拒绝"
                            llAuditTime.visible()
                            llRejectReason.visible()
                            tvAuditTime.text = ((it.check_time?:0) * 1000).toDateString("yyyy-MM-dd HH:mm")
                            tvRejectReason.text = it.remark
                        }
                    }
                }
            }
        }
    }

}